package models

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/model"
	"gorm.io/datatypes"

	"ssforward/common/dto"
	"ssforward/internal/forward/models/query"
)

type SysForwardServer struct {
	Id              string         `json:"id" gorm:"primaryKey;type:varchar(64);comment:id"`
	Name            string         `json:"name" gorm:"type:varchar(255);comment:转发服务名称"`
	Type            string         `json:"type" gorm:"type:varchar(10);comment:HTTP、MQTT、DB"`
	Host            string         `json:"host" gorm:"type:varchar(255);comment:主机地址"`
	Port            int            `json:"port" gorm:"type:int4;comment:端口"`
	Url             string         `json:"url" gorm:"type:varchar(255);comment:HTTP后置URL"`
	Username        string         `json:"username" gorm:"type:varchar(64);comment:用户名"`
	Password        string         `json:"password" gorm:"type:varchar(255);comment:密码"`
	DbName          string         `json:"dbName" gorm:"type:varchar(255);comment:DB数据库名"`
	DbDriver        string         `json:"dbDriver" gorm:"type:varchar(32);comment:DB驱动"`
	DbTable         string         `json:"dbTable" gorm:"type:varchar(255);comment:DB数据表名"`
	CreatorId       string         `json:"creatorId" gorm:"type:varchar(64);comment:创建人id"`
	AgencyId        string         `json:"agencyId" gorm:"type:varchar(64);comment:创建人机构用户id"`
	CreateTime      *dto.LocalTime `json:"createTime" gorm:"type:timestamptz(6);default:CURRENT_TIMESTAMP;comment:创建时间"`
	UpdateTime      *dto.LocalTime `json:"updateTime" gorm:"type:timestamptz(6);comment:更新时间"`
	ConnectStatus   string         `json:"connectStatus" gorm:"type:varchar(10);comment:连接状态/末次连接状态"`
	LastForwardTime *dto.LocalTime `json:"lastForwardTime" gorm:"type:timestamptz(6);comment:末次转发时间"`
	ForwardCount    int            `json:"forwardCount" gorm:"type:int4;comment:转发次数"`
	ContentType     string         `json:"contentType" gorm:"type:varchar(64);comment:content-type"`
	Header          datatypes.JSON `json:"header"  gorm:"type:jsonb;comment:请求头扩展内容"`
	model.Model
}

func (SysForwardServer) TableName() string {
	return "sys_forward_server"
}

func (e *SysForwardServer) Generate() interface{} {
	o := *e
	return &o
}

func (e *SysForwardServer) GetId() interface{} {
	return e.Id
}

func (e *SysForwardServer) FindServerList(req *query.ForwardServerQuery) (err error, result interface{}) {
	list := make([]SysForwardServer, 0)
	var count int64
	if req.AgencyId != req.UserId {
		// 非租户查无数据
		if req.PageIndex > 0 && req.PageSize > 0 {
			result = &dto.Page{
				Records:   list,
				Total:     count,
				PageSize:  req.GetPageSize(),
				PageIndex: req.GetPageIndex(),
				Pages:     (count + int64(req.GetPageSize()) - 1) / int64(req.GetPageSize()),
				Other:     nil,
			}
		} else {
			result = list
		}
		return
	}
	if req.PageIndex > 0 && req.PageSize > 0 {
		err = e.FindPage(&list, &count,
			dto.MakeCondition(req.GetNeedSearch()),
			dto.OrderDest("create_time", true, req.OrderBy),
			dto.Paginate(req.GetPageSize(), req.GetPageIndex()),
		)
		result = &dto.Page{
			Records:   list,
			Total:     count,
			PageSize:  req.GetPageSize(),
			PageIndex: req.GetPageIndex(),
			Pages:     (count + int64(req.GetPageSize()) - 1) / int64(req.GetPageSize()),
			Other:     nil,
		}
	} else {
		err = e.FindList(&list,
			dto.MakeCondition(req.GetNeedSearch()),
			dto.OrderDest("create_time", true, req.OrderBy),
		)
		result = list
	}
	return
}

func (e *SysForwardServer) FindServer(req *query.ForwardServerQuery) (err error, one SysForwardServer) {
	err = e.FindOne(&one, dto.MakeCondition(req.GetNeedSearch()))
	return
}
